From: Florian Müllner Date: Tue, 2 Jul 2019 03:24:29 +0000 (+0200) Subject: params: Simplify code X-Git-Tag: archive/raspbian/50.5-1+rpi1~1^2~845^2^2~70 X-Git-Url: https://dgit.raspbian.org/%22http://www.example.com/cgi/%22/%22http:/www.example.com/cgi/%22?a=commitdiff_plain;h=46874eed05b9bb9e1ad8a5eee469c1f6eacb323f;p=gnome-shell.git params: Simplify code Standard javascript now has Object.assign() which is very similar to Params.parse(), except that the latter by default disallows "extra" parameters. We can still leverage the standard API by simply implementing the error check, and then call out to Object.assign() for the actual parameter merging. https://gitlab.gnome.org/GNOME/gnome-shell/merge_requests/612 --- diff --git a/js/misc/params.js b/js/misc/params.js index 04691ba6a..0e3af9532 100644 --- a/js/misc/params.js +++ b/js/misc/params.js @@ -14,22 +14,12 @@ // // Return value: a new object, containing the merged parameters from // @params and @defaults -function parse(params, defaults, allowExtras) { - let ret = {}, prop; - - if (!params) - params = {}; - - for (prop in params) { - if (!(prop in defaults) && !allowExtras) - throw new Error('Unrecognized parameter "' + prop + '"'); - ret[prop] = params[prop]; - } - - for (prop in defaults) { - if (!(prop in params)) - ret[prop] = defaults[prop]; +function parse(params = {}, defaults, allowExtras) { + if (!allowExtras) { + for (let prop in params) + if (!(prop in defaults)) + throw new Error(`Unrecognized parameter "${prop}"`); } - return ret; -} \ No newline at end of file + return Object.assign(defaults, params); +}